//
//  TabbarButton.swift
//  微博项目
//
//  Created by admin on 16/3/2.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class TabbarButton: UIButton {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    var tabbar:Tabbar!
        {
        didSet
            {
                self.layoutSubviews()
            }
    }
    let badge = BadgeButton(type: UIButtonType.Custom)
    override var selected:Bool
        {
        willSet(newValue)
        {
            super.selected = newValue
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.badge)
        self.bringSubviewToFront(badge)
        self.adjustsImageWhenHighlighted = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    convenience init(tabbar:Tabbar)
    {
        self.init()
        self.tabbar = tabbar
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.setImage(UIImage(imageLiteral: tabbar.image), forState: UIControlState.Normal)
        self.setImage(UIImage(imageLiteral: tabbar.selectedImage), forState: UIControlState.Selected)
        self.setTitle(tabbar.title, forState: UIControlState.Normal)
        
        self.imageView?.contentMode = UIViewContentMode.Center
        let selfW = self.bounds.size.width
        let selfH = self.bounds.size.height
        self.imageView?.frame = CGRectMake(0, 0, selfW, selfH * 0.7)
        
        self.titleLabel?.frame = CGRectMake(0, CGRectGetMaxY((self.imageView?.frame)!), selfW, selfH * 0.3)
        self.titleLabel?.textAlignment = NSTextAlignment.Center
        
        self.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        self.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Selected)
        self.titleLabel?.font = self.titleLabel?.font.fontWithSize(11)
        
//        self.badge.badge = 1
        self.badge.frame = CGRectMake(selfW - self.badge.bounds.size.width, 0, self.badge.bounds.size.width, self.badge.bounds.size.height)
    }
}
